CE.PLACE = {

    // a cached reference to the currently active parent theme.
    active_place: null,
    init: function () {
        // re-structure places into a nice parent-child relationship.
        // note that we subtract one from place indices fetched from the database 
        // because they are zero-based in javascript arrays
        // but one-based in the SQL database.
        var sub_place_id, places = CE.PLACE.places,
            place_id;
        for (place_id in places) {
            for (var sub_place_num = 0; sub_place_num < places[place_id].sub_places.length; sub_place_num++) {
                sub_place_id = places[place_id].sub_places[sub_place_num];
                places[sub_place_id].parent_place = places[place_id];
            }
        }
        this.places = places;
        // a cached reference to the actual menu element.
        this.menu.$ = $("#ce_navigation_menu");
        this.menu.render();
    },

    menu: {
        click: function (evt) {
            // handle place menu clicks.
            var m = evt.target.id.match(/place-(\d+)/);
            if (m && m.length) {
                CE.PLACE.set(m[1]);
            }
        },

        // generates a menu item, highlighted if active.
        item: function (place) {
            var $item = $.touch('div').attr({
                id: 'place-' + place.id
            }).text(place.title);
            if (place.active) {
                place.active && $item.addClass('active');
            }
            return $item;
        },

        // insert all places in to the menu and sort by title
        render: function () {
            var k, sk = CE.UTIL.sorted_keys(CE.PLACE.places, 'title');
            this.$.empty();
            for (var i in sk) {
            	if (sk.hasOwnProperty(i)) {
	                k = sk[i];
	                if (!CE.PLACE.places[k].parent_place) {
	                    this.$.append(this.item(CE.PLACE.places[k]));
	                }
                }
            }
        },

        // highlights active place(s).
        activate: function () {
            var sub_place, place = CE.PLACE.active_place;
            $("#ce_menu_map").text(CE.PLACE.active_place.title);
            $('div.active', this.$).removeClass('active');
            $('#place-' + place.id).addClass('active');
            // clear existing sub places.
            if (!place.parent_place) {
	            $('div div', this.$).remove();
	            for (var i = 0; i < place.sub_places.length; i++) {
	                sub_place = CE.PLACE.places[place.sub_places[i]];
	                sub_place.active = true;
	                $('#place-' + place.id).append(this.item(sub_place));
	            }
            }
            if (place.sub_places.length === 0) {
                CE.WIDGETS.MENU.close_all();
            }
        }
    },

    // alter the currently active set of places.
    set: function (place_id) {
        var $place_tab,
        // themes array is offset from database id by 1.
        place = this.active_place = this.places[place_id];
        for (var i in this.places) {
            this.places[i].active = false;
        }
        place.active = true;

        // make all sub places of the active place also active.
        this.menu.activate();
        if (CE.map) {
            CE.map.zoomToExtent(CE.UTIL.bounds(
                this.active_place.western_longitude, 
                this.active_place.northern_latitude, 
                this.active_place.eastern_longitude, 
                this.active_place.southern_latitude
            ));
        }

        $('#ce_menu_map').text(this.active_place.title);

    }
};

